/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package kimo.bl;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import javax.persistence.EntityManagerFactory;
import javax.persistence.NoResultException;
import javax.persistence.Persistence;
import static kimo.bl.zkUtils.throwWrongValueException;
import kimo.persistence.controllers.DaokJpaController;
import kimo.persistence.controllers.UserJpaController;
import kimo.persistence.entities.Daok;
import kimo.persistence.entities.User;
import labels.LabelsEnum;
import org.zkoss.util.resource.Labels;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Session;
import org.zkoss.zk.ui.Sessions;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Textbox;

/**
 *
 * @author a381u296
 */
public class Register extends SelectorComposer<Component> {
    
    @Wire
    Textbox username;
    @Wire
    Textbox password;
    
    @Wire
    Textbox password_verify;
    @Wire
    Textbox user_first_name;
    @Wire
    Textbox user_last_name;
    @Wire
    Textbox user_phone;
    @Wire
    Textbox user_email;
    @Wire
    Listbox user_daok_cd;
    
    ListModelList<Daok> lmlDaoks;
    
    KimoDAO rdao;
    
    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp); //To change body of generated methods, choose Tools | Templates.

        rdao = new KimoDAO();
        
        lmlDaoks = new ListModelList<Daok>(rdao.getDaoks());
        
        user_daok_cd.setModel(lmlDaoks);
        
    }
    
    void populateListbox() {
        
    }
    
    @Listen("onClick = #registerBtn")
    public void onRegisterBtnClick() {
        
        Set<Daok> daoks = lmlDaoks.getSelection();
        if (daoks.isEmpty()) {
            zkUtils.throwWrongValueException(user_daok_cd, "Πρεπει να επιλέξετε ΔΑΟΚ");
        } else {
            Daok daok = daoks.iterator().next();
            if (password.getText().equals(password_verify.getText())) {
                User user = new User();
                user.setUserAdmin(false);
                user.setUsername(username.getText());
                user.setPassword(password.getText());
                user.setUserFirstName(user_first_name.getText());
                user.setUserLastName(user_last_name.getText());
                user.setUserEmail(user_email.getText());
                user.setUserPhone(user_phone.getText());
                user.setUserDaokCd(daok);
                String insertUser = rdao.insertUser(user);
                if (insertUser.equals("ok")) {
                    //Executions.sendRedirect("/index.zul");
                    ClearUI();
                    Messagebox.show("Ο χρήστης δημιουργήθηκε, μπορείτε να κανετε login");
                    
                } else {
                    if (insertUser.equals("username_exists")) {
                        throwWrongValueException(username, "To username υπάρχει ήδη");
                    } else {
                        if (insertUser.equals("email_exists")) {
                            throwWrongValueException(user_email, "To email υπάρχει ήδη");
                        }
                    }
                }
                
            } else {
                Messagebox.show("Δεν συμφωνούν οι κωδικοι");
                throwWrongValueException(password, "O Κωδικός δεν ειναι ίδιος");
                throwWrongValueException(password_verify, "O Κωδικός δεν ειναι ίδιος");
            }
        }
    }
    
    private void ClearUI() {
        
        username.setRawValue("");
        password.setRawValue("");
        password_verify.setRawValue("");
        user_first_name.setRawValue("");
        
        user_last_name.setRawValue("");
        user_phone.setRawValue("");
        user_email.setRawValue("");
        user_daok_cd.clearSelection();
        lmlDaoks.clearSelection();
    }
    
}
